package com.hxc.common.controller;

import com.hxc.common.bean.ZdMarkDown;
import com.hxc.common.common.ResultVO;
import com.hxc.common.db.HanaDB;
import com.hxc.common.service.MarkDownV32Service;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/markDownV3")
public class MarkDownV3Controller {

  @Autowired
  HanaDB hanaDB;

  @GetMapping("/searchProblem")
  public ResultVO<List<ZdMarkDown>> searchMarkdown(@RequestParam("searchKey") String searchKey,
      @RequestParam("searchSetting") String searchSetting)
      throws Exception {
    // 获取Git仓库中的Markdown文档列表
    try (Connection conn = hanaDB.create()) {

      return new ResultVO<>(0, "OK",
          new MarkDownV32Service().findFileBySearchKey(searchKey, searchSetting, conn));
    } catch (Exception e) {
      return new ResultVO<>(1, e.getMessage());
    }
  }


  @GetMapping("test")
  public ResultVO<List<String>> test() throws Exception {
//        List<String> filepathList = new MarkDownV32Service().getGitDataFilePath();
    new MarkDownV32Service().markdownEnter();
    return new ResultVO<>(new ArrayList<>());
  }

  @PostMapping("findMarkDownBypath")
  public ResultVO<String> findMarkDownBypath(@RequestBody ZdMarkDown zdMarkDown) throws Exception {
    try {
      return new ResultVO<>(0, "OK",
          new MarkDownV32Service().findMarkDownWithImageByData(zdMarkDown));
    } catch (Exception e) {
      return new ResultVO<>(1, e.getMessage());
    }
  }

  @PostMapping("addKeyWord")
  public ResultVO<String> addKeyWord(@RequestBody ZdMarkDown zdMarkDown) throws Exception {
    try (Connection conn = hanaDB.create()) {
      return new ResultVO<>(0, "OK", new MarkDownV32Service().addKeyWord(zdMarkDown, conn));
    } catch (Exception e) {
      return new ResultVO<>(1, e.getMessage());
    }
  }

  @PostMapping("deleteKeyWord")
  public ResultVO<String> deleteKeyWord(@RequestBody ZdMarkDown zdMarkDown) throws Exception {
    try (Connection conn = hanaDB.create()) {
      return new ResultVO<>(0, "OK", new MarkDownV32Service().deleteKeyWord(zdMarkDown, conn));
    } catch (Exception e) {
      return new ResultVO<>(1, e.getMessage());
    }
  }

  @GetMapping("findMarkDownByKey")
  public ResultVO<ZdMarkDown> findMarkDownByKey(@RequestParam("handle") String handle) throws Exception {
    try (Connection conn = hanaDB.create()) {
      return new ResultVO<>(0, "OK", new MarkDownV32Service().findMarkDownByKey(handle, conn));
    } catch (Exception e) {
      return new ResultVO<>(1, e.getMessage());
    }
  }
}
